#!/usr/bin/env python
import pika


class LinkMq:
    def __init__(self):
        self.auth = pika.PlainCredentials('admin', 'qazwsx123')
        self.connection = pika.BlockingConnection(pika.ConnectionParameters('123.56.30.123', 5682, '/', self.auth))

    def put_mq(self, messages):

        channel = self.connection.channel()

        channel.queue_bind(queue='pdfSplit', exchange="entInfo")

        channel.basic_publish(exchange='entInfo',
                              routing_key='pdfSplit',
                              body=messages)
        print(f" [x] Sent {messages}")
        self.connection.close()

    def callback(self, ch, method, properties, body):
        print(" [x] ch %r" % ch)
        print(" [x] method %r" % method)
        print(" [x] properties %r" % properties)
        print(" [x] body %r" % body)

    def get_mq(self, callback=None, exchange='', queue=''):
        channel = self.connection.channel()
        channel.queue_bind(queue=queue, exchange=exchange)
        channel.basic_consume(on_message_callback=callback,
                              queue=queue,
                              auto_ack=True)
        # s = channel.basic_get(queue='pdfSplit', auto_ack=True)
        # print(s)
        # print(' [*] Waiting for messages. To exit press CTRL+C')
        channel.start_consuming()


if __name__ == '__main__':
    def callback(ch, method, properties, body):
        print(" [x] ch %r" % ch)
        print(" [x] method %r" % method)
        print(" [x] properties %r" % properties)
        print(eval(str(body, 'utf-8')))
    lk = LinkMq()
    # co_ids = [
    #     95757,
    #     96128,
    #     121293,
    #     790043,
    #     369937
    # ]
    # for i in co_ids:
    # lk.put_mq(str({"co_id": 379094, "type": "0", "year": 2021}))
    lk.put_mq(str({"co_id": 987946, "type": "0", "year": 2021}))
    # lk.get_mq(callback=callback, queue='pdfSplit', exchange="entInfo")
